#!/usr/bin/python3
# -*- coding:utf-8 -*-
"""
@author: 
@time: 2024/3/2 12:01 AM
@Function: 存放asset页面对象
"""
import time

import allure
from playwright.sync_api import Page, expect

from pages.asset_page.asset_bim_page import AssetPage
from repository import assetGisSelectObject, assetGisBimObject, AddGisAssetObject, AssetQueryObject, \
    AssetGisConfirmObject, assetListObject, AssetGisListObject, assetMoreObject


class AssetGisPage:
    def __init__(self, page: Page):
        self.page = page
        self.assetPage = AssetPage(page)
        self.asset_bim_tab_loc = page.locator(assetGisBimObject.asset_bim_tab)
        self.asset_gis_tab_loc = page.locator(assetGisBimObject.asset_gis_tab)
        # 添加gis资产
        self.asset_gis_add_loc = page.locator(AddGisAssetObject.add_gisasset)
        self.asset_gis_type_loc = page.locator(AddGisAssetObject.choice_asset_type)
        self.asset_gis_type_list_box_loc = page.locator(AddGisAssetObject.asset_type_list_box)
        self.asset_gis_origin_loc = page.locator(AddGisAssetObject.choice_asset_origin)
        self.asset_gis_upload_file_loc = page.locator(AddGisAssetObject.choice_asset_upload_file)
        self.asset_gis_name_loc = page.get_by_placeholder(AddGisAssetObject.add_gisasset_name)
        self.asset_gis_desc_loc = page.get_by_placeholder(AddGisAssetObject.add_gisasset_desc)
        self.asset_gis_button_loc = page.get_by_role('button', name=AddGisAssetObject.add_gisasset_button)
        self.asset_gis_upload_box_loc = page.locator(AddGisAssetObject.asset_upload_box)
        self.asset_epsg_loc=page.get_by_placeholder(AddGisAssetObject.asset_epsg)
        self.asset_epsg_select_loc=page.locator(AddGisAssetObject.asset_epsg_select)
        # gis列表相关页面对象操作
        self.gis_asset_more_list_loc=page.locator(AssetGisListObject.gis_asset_more_list)
        self.gis_asset_more_loc=page.locator(AssetGisListObject.gis_asset_more)
        # gis资产相关弹框
        self.gis_asset_convert_confirm_loc=page.locator(AssetGisConfirmObject.gis_asset_convert_confirm)
        self.gis_asset_convert_cancel_loc=page.locator(AssetGisConfirmObject.gis_asset_convert_cancel)

        # gis资产查询相关页面对象操作
        self.asset_gis_queryAssetType_loc = page.locator(AssetQueryObject.querySet_assetType_box)
        self.asset_gis_assetTypeAndStatus_loc = page.locator(AssetQueryObject.querySet_assetTypeAndStatus_content)


    @allure.step('添加gis资产时，选择资产类型')
    def add_gis_asset_type(self, asset_type='bim', asset_type_text="倾斜摄影模型"):
        asset_type_list_info = []
        if asset_type != 'bim':
            self.asset_gis_tab_loc.click()
        self.asset_gis_add_loc.click()
        self.asset_gis_type_loc.click()
        self.asset_gis_type_list_box_loc.wait_for(state="visible")
        asset_type_counts = self.page.locator(AddGisAssetObject.asset_type_list).count()
        asset_type_list = self.page.query_selector_all(AddGisAssetObject.asset_type_list)
        for asset in range(0, len(asset_type_list)):
            asset_type_list_info.append(asset_type_list[asset].inner_text())
        # print("asset_type_counts数量为：", asset_type_counts)
        # print("--->",asset_type_list_info)
        try:
            for x in range(1, asset_type_counts + 1):
                asset_type = self.page.locator(AddGisAssetObject.asset_type_list + "[{}]".format(x))
                if asset_type.inner_text() == asset_type_text:
                    asset_type.click()
                    asset_type_list_info.remove(asset_type_text)
                    break
                elif asset_type.inner_text() != asset_type_text and asset_type.inner_text() in asset_type_list_info and asset_type.inner_text() == asset_type_text:
                    asset_type.click()
                    break
        except Exception as e:
            print(e)

    #暂时留着不用，目前默认只有本地上传
    @allure.step('添加gis资产时，选择资产来源')
    def add_gis_asset_origin(self):
        self.asset_gis_origin_loc.click()

    @allure.step('添加gis资产时，选择资产文件')
    def upload_gis_file(self, asset_file):
        self.page.set_input_files(AddGisAssetObject.choice_asset_upload_file, asset_file)

    @allure.step('添加点云gis资产时，输入空间参考')
    def add_asset_epsg(self,epsg_name):
        self.asset_epsg_loc.fill(epsg_name)
        self.asset_epsg_select_loc.click()

    @allure.step('添加gis资产时，输入资产名称和描述，并执行添加')
    def add_gis_assetInfo(self, gis_name, gis_desc):
        self.asset_gis_name_loc.fill(gis_name)
        self.asset_gis_desc_loc.fill(gis_desc)
        self.asset_gis_button_loc.click()
        self.asset_gis_upload_box_loc.wait_for(state="visible")
        self.asset_gis_upload_box_loc.wait_for(state="hidden")

    @allure.step('勾选gis资产查询设置中的资产内容')
    def gis_asset_query_setting_assetType(self, asset_type_name):
        self.asset_gis_queryAssetType_loc.click()
        asset_type_lists = self.asset_gis_assetTypeAndStatus_loc.count()
        for x in range(1, asset_type_lists + 1):
            if self.page.locator(AssetQueryObject.querySet_assetTypeAndStatus_content + "[{}]".format(
                    x)).inner_text().strip() == asset_type_name:
                self.page.locator(AssetQueryObject.querySet_assetTypeAndStatus_content + "[{}]".format(
                    x)).click()
                break

    @allure.step('点击gis资产转换')
    def click_gis_asset_convert(self, asset_list_index):
        self.page.locator(
            '//tbody[@class="ant-table-tbody"]/tr[{}]/td[6]//button[1]'.format(asset_list_index)).click()

    @allure.step('转换gis资产')
    def asset_convert_confirm_cancel_button(self, is_convert):
        try:
            self.asset_gis_add_loc.wait_for(state='visible')
            asset_numbers = self.page.locator('//tbody[@class="ant-table-tbody"]/tr').count()
            for x in range(1, asset_numbers + 1):
                gis_asset_status = self.page.locator(
                    '//tbody[@class="ant-table-tbody"]/tr[{}]/td[4]/span'.format(x)).inner_text()
                if gis_asset_status in ['待转换', '转换失败', '转换成功']:
                    self.click_gis_asset_convert(x)
                    if is_convert is True:
                        self.gis_asset_convert_confirm_loc.wait_for(state="visible")
                        self.gis_asset_convert_confirm_loc.click()
                        self.gis_asset_convert_confirm_loc.wait_for(state='hidden')
                        while True:
                            asset_status = self.page.locator(
                                '//tbody[@class="ant-table-tbody"]/tr[{}]/td[4]/span'.format(x)).inner_text()
                            if asset_status == '转换成功':
                                break
                    else:
                        self.gis_asset_convert_cancel_loc.click()
                        break
                    print('资产状态为:{}'.format(asset_status))
                    break
        except Exception as e:
            print(f"转换gis资产出现异常，请查看：{e}")

